import java.util.ArrayList;

/**
 * Created by coy on 2017/9/11.
 */
public class Table {
    private static final double[][] PRICE = {
            {0,0,0,0,0,0,0,0,0,30,30,30,50,50,50,50,50,50,80,80,60,60,0,0},
            {0,0,0,0,0,0,0,0,0,40,40,40,50,50,50,50,50,50,60,60,60,60,0,0}
    };
    private static final double[] FINE = {0.5, 0.25};

    private String[] userList;   //预定的用户名
    private double[] timeList; //当前时刻是否预定
    private int priceIndex;     //周（1-5）或周末

    public Table(String user, int startTime, int endTime, int index){
        userList = new String[24];
        timeList = new double[24];
        priceIndex = index;

        for(int i = startTime;i < endTime;i++){
            userList[i] = user;
            timeList[i] = 1;
        }
    }

    public boolean book(String user, int startTime, int endTime){
        for(int i = startTime;i < endTime;i++){
            if(timeList[i] != 0)
                return false;
            timeList[i] = 1;
            userList[i] = user;
        }
        return true;
    }

    public boolean cancel(String user, int startTime, int endTime){
        for(int i = startTime;i < endTime;i++){
            if(!user.equals(userList[i]) || timeList[i] != 1)
                return false;
            timeList[i] = FINE[priceIndex];
        }
        return true;
    }

    public ArrayList<String> getResult(){
        ArrayList<String> results = new ArrayList<>();
        int j = 9;
        while(j < 23){
            double sum;
            if(timeList[j] == 1){
                sum = 0;
                StringBuffer sb = new StringBuffer();
                sb.append(" ").append(j).append(":00~");
                while(j < 23 && timeList[j] == 1){
                    sum += timeList[j]*PRICE[priceIndex][j];
                    j++;
                }
                sb.append(j).append(":00 ").append(sum).append("元");
                results.add(sb.toString());
            }
            if(timeList[j] > 0 && timeList[j] < 1 ){
                sum = 0;
                StringBuffer sb = new StringBuffer();
                sb.append(" ").append(j).append(":00~");
                while(j < 23 && (timeList[j] > 0 || timeList[j] < 1)){
                    sum += timeList[j]*PRICE[priceIndex][j];
                    j++;
                }
                sb.append(j).append(":00 违约金 ").append(sum).append("元");
                results.add(sb.toString());
            }
            j++;
        }
        return results;
    }

    public double getTotal(){
        double sum = 0;
        for(int i = 9;i < 22;i++){
            sum += timeList[i]*PRICE[priceIndex][i];
        }
        return sum;
    }

}
